package com.base.exp

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Parcelable
import java.io.Serializable

fun Intent?.stringExtra(name: String): String {
    if (null == this)
        return ""
    return getStringExtra(name) ?: ""
}

fun Intent?.boolExtra(name: String, defValue: Boolean): Boolean {
    if (null == this)
        return defValue
    return getBooleanExtra(name, defValue)
}

fun Intent?.intExtra(name: String, defValue: Int): Int {
    if (null == this)
        return defValue
    return getIntExtra(name, defValue)
}

fun <T : Serializable> Intent?.serializable(name: String, defValue: T): T? {
    if (null == this)
        return defValue

    val value = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        getSerializableExtra(name, defValue::class.java)
    } else {
        getSerializableExtra(name) as? T?
    }
    return value ?: defValue
}

fun <T : Parcelable> Intent?.parcelable(name: String, tCls: Class<T>): T? {
    if (null == this)
        return null
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        getParcelableExtra(name, tCls)
    } else {
        getParcelableExtra(name)
    }
}

fun Bundle?.boolExtra(name: String, defValue: Boolean): Boolean {
    if (null == this)
        return defValue
    return getBoolean(name, defValue)
}